package com.zm.tools.generatebackcode.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;

import static com.zm.tools.util.core.generatecode.commonnew.PackageNameUtil.getPomPackage;

/**
 * @author zoum
 * @create 2018/10/25 17:14
 */
public class GeneratePomCode {

    private final static Logger LOGGER = LoggerFactory.getLogger(GeneratePomCode.class);

    public static void generate(String baseFilePath, String tableName, String companyName, String appName, Integer type, String modelName) throws IOException{
        String pakage = getPomPackage(companyName, appName);
        generatePomXml(baseFilePath, tableName, appName, modelName, pakage, type);
    }

    private static void generatePomXml(String baseFilePath, String tableName, String appName, String modelName, String pakage, Integer type) throws IOException{
        LOGGER.info("generatePomXml start for " + tableName);
        String parentPath = baseFilePath + "\\" + appName;
        if (modelName != null){
           parentPath = parentPath + "\\"+ appName + "-" + modelName;
        }

        File parentFile  = new File(parentPath);
        if (!parentFile.exists()){
            parentFile.mkdirs();
        }
        String xmlName = "pom.xml";
        File xmlFile = new File(parentFile.getPath(), xmlName);
        if (xmlFile.exists()){
            xmlFile.delete();
        }
        xmlFile.createNewFile();

        FileWriter fw = new FileWriter(xmlFile);
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
                .append("<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\n")
                .append("  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n")
                .append("  xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n")
                .append("   <modelVersion>4.0.0</modelVersion>\n")
        ;
        if (type == null || type == 0){
            sb.append("   <groupId>"+ pakage +"</groupId>\n")
                    .append("   <packaging>"+ ((type == null || type == 0) ? "pom" : "jar" )+"</packaging>\n")
                    .append("   <version>1.0.0-SNAPSHOT</version>\n")
                    .append("   <artifactId>"+ appName + (modelName == null ? "" : "-" + modelName) +"</artifactId>\n")
                    .append("   <modules>\n")
                    .append("       <module>"+ appName + "-facade" + "</module>\n")
                    .append("       <module>"+ appName + "-model" + "</module>\n")
                    .append("       <module>"+ appName + "-manager" + "</module>\n")
                    .append("   </modules>\n\n")
                    .append(
                            "   <properties>\n" +
                                    "       <java.version>1.8</java.version>\n" +
                                    "       <fastjson.version>1.2.36</fastjson.version>\n" +
                                    "       <spring.version>5.0.4.RELEASE</spring.version>\n" +
                                    "       <rongzhijia.tools.version>0.0.1-SNAPSHOT</rongzhijia.tools.version>\n" +
                                    "   </properties>\n" +
                    "   <dependencyManagement>\n" +
                            "       <dependencies>\n" +
                            "           <dependency>\n" +
                            "                <groupId>org.springframework</groupId>\n" +
                            "                <artifactId>spring-web</artifactId>\n" +
                            "                <version>${spring.version}</version>\n" +
                            "            </dependency>\n" +
                            "            <dependency>\n" +
                            "                <groupId>org.springframework</groupId>\n" +
                            "                <artifactId>spring-webmvc</artifactId>\n" +
                            "                <version>${spring.version}</version>\n" +
                            "            </dependency>\n" +
                            "            <dependency>\n" +
                            "                <groupId>org.springframework</groupId>\n" +
                            "                <artifactId>spring-core</artifactId>\n" +
                            "                <version>${spring.version}</version>\n" +
                            "            </dependency>\n" +
                            "            <dependency>\n" +
                            "                <groupId>org.springframework</groupId>\n" +
                            "                <artifactId>spring-context-support</artifactId>\n" +
                            "                <version>${spring.version}</version>\n" +
                            "            </dependency>\n" +
                            "            <dependency>\n" +
                            "                <groupId>org.springframework</groupId>\n" +
                            "                <artifactId>spring-beans</artifactId>\n" +
                            "                <version>${spring.version}</version>\n" +
                            "            </dependency>\n" +
                            "            <dependency>\n" +
                            "                <groupId>org.springframework</groupId>\n" +
                            "                <artifactId>spring-test</artifactId>\n" +
                            "                <version>${spring.version}</version>\n" +
                            "           </dependency>\n" +
                            "       </dependencies>\n" +
                            "   </dependencyManagement>\n")

                    .append(
                    "   <build>\n" +
                    "       <plugins>\n" +
                    "            <plugin>\n" +
                    "                <groupId>org.apache.maven.plugins</groupId>\n" +
                    "                <artifactId>maven-compiler-plugin</artifactId>\n" +
                    "                <configuration>\n" +
                    "                    <target>${java.version}</target>\n" +
                    "                    <source>${java.version}</source>\n" +
                    "                </configuration>\n" +
                    "            </plugin>\n" +
                    "       </plugins>\n" +
                    "   </build>\n");

        }else {
            sb.append(
                    "   <parent>\n" +
                    "       <artifactId>"+ appName +"</artifactId>\n" +
                    "       <groupId>"+ pakage +"</groupId>\n" +
                    "       <version>1.0.0-SNAPSHOT</version>\n" +
                    "   </parent>\n")
                    .append("   <artifactId>"+ appName + (modelName == null ? "" : "-" + modelName) +"</artifactId>\n");
            sb.append(
                    "   <dependencies>\n");
            String applicationName = appName.split("-").length>1 ? appName.split("-")[1] : appName;
            if (Objects.equals(modelName, "model")){
                sb.append(
                        "       <dependency>\n" +
                        "           <groupId>com.rongzhijia.jdq.basics."+ applicationName +"</groupId>\n" +
                        "           <artifactId>basics-"+ applicationName +"-model</artifactId>\n" +
                        "           <version>0.0.1-SNAPSHOT</version>\n" +
                        "           <scope>provided</scope>\n" +
                        "       </dependency>\n")
                .append("       <dependency>\n" +
                        "           <groupId>org.springframework</groupId>\n" +
                        "           <artifactId>spring-beans</artifactId>\n" +
                        "       </dependency>\n");

            }else if (Objects.equals(modelName, "facade")){
                sb
                .append("       <dependency>\n" +
                        "           <groupId>org.springframework</groupId>\n" +
                        "           <artifactId>spring-context-support</artifactId>\n" +
                        "       </dependency>\n")
                .append(
                        "       <dependency>\n" +
                        "           <groupId>com.rongzhijia.jdq.basics."+ applicationName +"</groupId>\n" +
                        "           <artifactId>basics-"+ applicationName +"-api</artifactId>\n" +
                        "           <version>0.0.1-SNAPSHOT</version>\n" +
                        "       </dependency>\n")
                .append(
                        "       <dependency>\n" +
                        "           <groupId>"+ pakage +"</groupId>\n" +
                        "           <artifactId>back-"+ applicationName +"-model</artifactId>\n" +
                        "           <version>1.0.0-SNAPSHOT</version>\n" +
                        "       </dependency>\n")
                .append(
                        "       <dependency>\n" +
                        "           <groupId>com.alibaba</groupId>\n" +
                        "           <artifactId>dubbo</artifactId>\n" +
                        "           <version>2.5.3</version>\n" +
                        "           <exclusions>\n" +
                        "               <exclusion>\n" +
                        "                   <groupId>org.springframework</groupId>\n" +
                        "                   <artifactId>spring</artifactId>\n" +
                        "               </exclusion>\n" +
                        "               <exclusion>\n" +
                        "                   <groupId>javassist</groupId>\n" +
                        "                   <artifactId>javassist</artifactId>\n" +
                        "               </exclusion>\n" +
                        "           </exclusions>\n" +
                        "       </dependency>\n" +
                        "       <dependency>\n" +
                        "           <groupId>org.javassist</groupId>\n" +
                        "           <artifactId>javassist</artifactId>\n" +
                        "           <version>3.18.2-GA</version>\n" +
                        "       </dependency>\n" +
                        "       <dependency>\n" +
                        "           <groupId>org.apache.zookeeper</groupId>\n" +
                        "           <artifactId>zookeeper</artifactId>\n" +
                        "           <version>3.4.6</version>\n" +
                        "           <exclusions>\n" +
                        "               <exclusion>\n" +
                        "                   <groupId>log4j</groupId>\n" +
                        "                   <artifactId>log4j</artifactId>\n" +
                        "               </exclusion>\n" +
                        "               <exclusion>\n" +
                        "                   <groupId>org.slf4j</groupId>\n" +
                        "                   <artifactId>slf4j-api</artifactId>\n" +
                        "               </exclusion>\n" +
                        "               <exclusion>\n" +
                        "                   <groupId>org.slf4j</groupId>\n" +
                        "                   <artifactId>slf4j-log4j12</artifactId>\n" +
                        "               </exclusion>\n" +
                        "           </exclusions>\n" +
                        "       </dependency>\n" +
                        "       <dependency>\n" +
                        "           <groupId>com.101tec</groupId>\n" +
                        "           <artifactId>zkclient</artifactId>\n" +
                        "           <version>0.3</version>\n" +
                        "       </dependency>\n");
            }else if (Objects.equals(modelName, "manager")){
                sb.append(
                        "       <dependency>\n" +
                        "           <groupId>"+ pakage +"</groupId>\n" +
                        "           <artifactId>back-"+ applicationName +"-facade</artifactId>\n" +
                        "           <version>1.0.0-SNAPSHOT</version>\n" +
                        "       </dependency>\n")
                .append(
                        "       <dependency>\n" +
                        "           <groupId>com.rongzhijia.tools</groupId>\n" +
                        "           <artifactId>validate</artifactId>\n" +
                        "           <version>0.0.1-SNAPSHOT</version>\n" +
                        "       </dependency>\n");
            }
            sb.append(
                    "   </dependencies>\n");
        }

        sb.append("</project>\n")
        ;
        fw.write(sb.toString());
        fw.flush();
        fw.close();
        LOGGER.info("generatePomXml end for " + tableName);
    }

}
